<template>
    <div class="container">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px"
                 class="demo-ruleForm">
            <el-form-item label="电话" prop="p1">
                <el-input v-model="ruleForm2.p1" auto-complete="off" :disabled="uid!=''"></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="p2">
                <el-input v-model="ruleForm2.p2" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="昵称" prop="p3">
                <el-input v-model="ruleForm2.p3" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="p4">
                <template>
                    <el-radio v-model="ruleForm2.p4" label="1">先生</el-radio>
                    <el-radio v-model="ruleForm2.p4" label="2">女士</el-radio>
                </template>
            </el-form-item>
            <el-form-item label="角色" prop="p5">
                <el-select v-model="ruleForm2.p5" placeholder="选择角色" class="handle-select mr10">
                    <template v-for="v in roleList">
                        <el-option :label="v.name" :value="v.id"></el-option>
                    </template>
                </el-select>
            </el-form-item>
            <el-form-item label="密码" prop="p6" v-show="!uid">
                <el-input type="password" v-model="ruleForm2.p6" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="头像" prop="p7">
                <div>
                    <img :src="ruleForm2.p7" alt="" style="display:inline-block;vertical-align:bottom;width: 80px;height: 80px;border: 1px solid #eee;">
                    <el-button @click="upFile">选择</el-button>
                </div>
            </el-form-item>
            <br>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {quillEditor} from 'vue-quill-editor';
    import pub from '../../utils/pub'
    import md5 from 'md5'
    import upload from '@/components/common/Upload'

    export default {
        components: {
            quillEditor,
            upload,
        },
        data: function () {
            var v1 = (rule, value, callback) => {
                if (!this.config.isMobile(value)) {
                    callback(new Error('请输入正确的手机号'));
                } else {
                    if(this.uid){
                        callback();
                    }else{
                        this.regMobile(value);
                        callback();
                    }
                }
            };
            var v2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入姓名'));
                } else if (value < 2 || value.length > 10) {
                    callback(new Error('2-10位'));
                } else {
                    callback();
                }
            };
            return {
                roleList: [],
                isHasManager: false,
                admin: {},
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                ruleForm2: {
                    p1: '', // 电话
                    p2: '', // 姓名
                    p3: '', // 角色
                    p4: '1', // 性别
                    p5: '', // 头像
                    p6: '', // 头像
                    p7: '', // 头像
                },
                rules2: {
                    p2: [
                        {required: true, message: '请输入姓名', validator: v2, trigger: 'blur'}
                    ],
                    p5: [
                        {required: false, message: '输入6-16位密码', trigger: 'blur'}
                    ],
                }
            }
        },
        props: ['uid'],
        methods: {
            loadopen() {
                this.loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.2)'
                });
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {
                this.loading.close();
            },
            // 查询电话是否重复
            regMobile(mobile){
                this.config.post('/admin/admin/findUserByMobile', {mobile: mobile}).then(res=>{
                    if(res.data.code==='1'){
                        this.isHasManager = false;
                    }else {
                        if(res.data.code==='0'){
                            let user = res.data.user;
                            this.ruleForm2.p2 = user.name;
                            // this.ruleForm2.p4 = user.nick;
                            this.ruleForm2.p5 = user.pwd;
                            this.ruleForm2.p6 = user.sex;
                            this.ruleForm2.p7 = user.head_url;

                            this.isHasManager = true;
                        }else if(res.data.code==='-1001'){
                            this.ruleForm2.p1 = '';
                            this.isHasManager = false;
                            this.$message.error(this.config.handleError(res.data));
                        }else{
                            this.isHasManager = false;
                            this.$message.error(this.config.handleError(res.data));
                        }
                    }
                });
            },
            // 获取角色列表
            getRoleList(){
                this.config.post('/housinggestate/admin/role/findPage', {pageNum: '1'}).then(res=>{
                    if(res.data.code==='0'){
                        this.roleList = res.data.page.list;
                    }else {
                        this.$message.error(this.config.handleError(res.data));
                    }
                })
            },
            // 获取管理员信息
            getInfo() {
                this.clear();
                if(!this.uid) {
                    this.ruleForm2.p1 = '';
                    this.ruleForm2.p2 = '';
                    return;
                }
                this.loadopen();
                this.config.get('/housinggestate/admin/admin/findById', {id: this.uid}).then(res => {
                    this.loadclose();
                    if (res.data.code === '0') {
                        // console.log(res.data, '获取角色信息');
                        this.admin = res.data.checkHousinggestateAdmin;
                        this.ruleForm2.p1 = this.admin.mobile;
                        this.ruleForm2.p2 = this.admin.user_name;
                        this.ruleForm2.p3 = this.admin.user_nick;
                        this.ruleForm2.p4 = this.admin.sex;
                        this.ruleForm2.p5 = this.admin.role_id;
                        this.ruleForm2.p7 = this.admin.head_url;
                    } else {
                        this.$message.error(this.config.handleError(res.data));
                    }
                });
            },
            // 清空表单
            clear(){
                this.ruleForm2.p1 = '';
                this.ruleForm2.p2 = '';
                this.ruleForm2.p3 = '';
                this.ruleForm2.p4 = '';
                this.ruleForm2.p5 = '';
                this.ruleForm2.p6 = '';
                this.ruleForm2.p7 = '';
            },
            upFile(){
                let self = this;
                let el = document.createElement('input')
                el.type = 'file';el.accept="image/*"
                el.onchange = (e)=>{
                    self.ruleForm2.p7 = '';
                    self.loadopen()
                    pub.OssUpload(e.target.files[0], (res)=>{
                        self.loadclose()
                        // console.log(res, '哈哈，我拿到了')
                        self.ruleForm2.p7 = res;
                    })
                };
                el.click();
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let obj = {
                            'h.id': this.uid,
                            'h.housinggestate_id': this.config.globalInfo.user.housinggestate_id,
                            'h.role_id': this.ruleForm2.p5,
                            'u.id': this.admin.user_id,
                            'u.user_name': this.ruleForm2.p2,
                            'u.user_nick': this.ruleForm2.p3,
                            'u.sex': this.ruleForm2.p4,
                            'u.head_url': this.ruleForm2.p7,
                        };
                        // console.log(obj);
                        let url = this.uid ? '/housinggestate/admin/admin/update' : '/housinggestate/admin/admin/add';
                        if(!this.uid) obj['u.login_pwd'] = md5(this.ruleForm2.p6);
                        if(!this.uid) obj['u.mobile'] = md5(this.ruleForm2.p1);
                        this.loadopen();
                        this.config.post(url, obj).then(res => {
                            this.loadclose();
                            if (res.data.code === '0') {
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(this.config.handleError(res.data));
                            }
                            this.$emit('done', '');
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
        },
        mounted() {
            this.getRoleList()
        }
    }
</script>
